home *** CD-ROM | disk | FTP | other *** search
- 10 'KEYBOARD.BAS - From the GWBASIC Tutorial Series, #11
- 20 'October, 1991
- 30 '
- 40 'To illustrate key trapping, let's use a simple little program that just
- 50 'prints a message on the screen, changing the color every once in a while,
- 60 'then when one of the 'trapped' keys is pressed beeps and prints a message
- 70 'at the bottom of the screen.
- 80 '
- 90 'First, we define the 'user' keys:
- 100 '
- 110 KEY 16, CHR$(&H8) + CHR$(&H1E)
- 120 KEY 17, CHR$(&H8) + CHR$(&H20)
- 130 KEY 18, CHR$(&H8) + CHR$(&H21)
- 140 KEY 19, CHR$(&H8) + CHR$(&H19)
- 150 KEY 20, CHR$(&H0) + CHR$(&H1)
- 160 '
- 170 'Next, we need to turn on key trapping...
- 180 '
- 190 KEY(1) ON
- 200 KEY(13) ON
- 210 KEY(12) ON
- 230 KEY(16) ON
- 240 KEY(17) ON
- 250 KEY(18) ON
- 260 KEY(19) ON
- 270 KEY(20) ON
- 280 '
- 290 'Finally, tell the program what to do with these keys...
- 300 '
- 310 ON KEY(1) GOSUB 1000
- 320 ON KEY(13) GOSUB 2000
- 330 ON KEY(12) GOSUB 3000
- 350 ON KEY(16) GOSUB 5000
- 360 ON KEY(17) GOSUB 6000
- 370 ON KEY(18) GOSUB 7000
- 380 ON KEY(19) GOSUB 8000
- 390 ON KEY(20) GOSUB 9000
- 400 '
- 410 'Here's where your main program would go. Instead, we'll have some fun!
- 420 '
- 430 CLS:KEY OFF
- 440 RANDOMIZE (-TIMER)
- 450 TIME = RND(500)
- 460 LOCATE 15,1,0
- 470 COL = COL + 1
- 480 IF COL > 16 THEN COL = 1
- 485 COLOR COL,0,0
- 490 PRINT "I'm waiting for you to press a key..."
- 500 FOR N = 1 TO TIME: NEXT N
- 510 GOTO 450
- 1000 ' F1 key pressed - print a message
- 1010 LOCATE 20,1,0
- 1020 PRINT "The last key you pressed was the F1 key... "
- 1030 RETURN
- 2000 ' Right arrow pressed - print a message
- 2010 LOCATE 20,1,0
- 2020 PRINT "The last key you pressed was the right arrow key... "
- 2030 RETURN
- 3000 ' Left arrow pressed - print a message
- 3010 LOCATE 20,1,0
- 3020 PRINT "The last key you pressed was the left arrow key... "
- 3030 RETURN
- 5000 ' Alt-A key pressed - print a message
- 5010 LOCATE 20,1,0
- 5020 PRINT "The last key you pressed was the Alt-A key... "
- 5030 RETURN
- 6000 ' Alt-D key pressed - print a message
- 6010 LOCATE 20,1,0
- 6020 PRINT "The last key you pressed was the Alt-D key... "
- 6030 RETURN
- 7000 ' Alt-F key pressed - print a message
- 7010 LOCATE 20,1,0
- 7020 PRINT "The last key you pressed was the Alt-F key... "
- 7030 RETURN
- 8000 ' Alt-P key pressed - print a message
- 8010 LOCATE 20,1,0
- 8020 PRINT "The last key you pressed was the Alt-P key... "
- 8030 RETURN
- 9000 ' Escape key pressed - print a message
- 9010 LOCATE 20,1,0
- 9020 PRINT "The last key you pressed was the ESCAPE key... "
- 9030 RETURN